<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>get_table_data_to_csv (extraction.rb)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File extraction.rb, line 42</span>
 <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_table_data_to_csv</span>(<span class="ruby-identifier">connection_options</span>)
        <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{connection_options[0]}&quot;</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot; &quot;</span><span class="ruby-operator">+</span><span class="ruby-node">&quot;#{connection_options[1]}&quot;</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot; &quot;</span><span class="ruby-operator">+</span><span class="ruby-node">&quot;#{connection_options[2]}&quot;</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot; &quot;</span><span class="ruby-operator">+</span><span class="ruby-node">&quot;#{connection_options[3]}&quot;</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot; &quot;</span><span class="ruby-operator">+</span><span class="ruby-node">&quot;#{connection_options[4]}&quot;</span>
        <span class="ruby-identifier">table_counter</span> = <span class="ruby-value">0</span>
        <span class="ruby-identifier">new_connection</span> = <span class="ruby-identifier">create_new_connection</span>(<span class="ruby-identifier">connection_options</span>)
        <span class="ruby-identifier">all_table_names</span> = <span class="ruby-identifier">new_connection</span>.<span class="ruby-identifier">query</span>(<span class="ruby-value str">'show tables'</span>)
        <span class="ruby-ivar">@start_time_database</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">new</span>
        <span class="ruby-identifier">all_table_names</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">table_name</span><span class="ruby-operator">|</span>
                <span class="ruby-identifier">create_structure_csv_file</span>(<span class="ruby-identifier">table_name</span>,<span class="ruby-identifier">new_connection</span>)
                <span class="ruby-identifier">generate_header_file</span>(<span class="ruby-identifier">table_name</span>,<span class="ruby-identifier">new_connection</span>)
                <span class="ruby-ivar">@start_time</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> 
                <span class="ruby-identifier">file_handler</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;#{table_name}.csv&quot;</span>,<span class="ruby-value str">'w'</span>)
                        <span class="ruby-ivar">@data_from_each_row</span> = <span class="ruby-identifier">new_connection</span>.<span class="ruby-identifier">query</span>(<span class="ruby-node">&quot;select * from #{table_name}&quot;</span>)
                        <span class="ruby-ivar">@data_from_each_row</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">data_record</span><span class="ruby-operator">|</span>
                                <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">single_field_value</span> <span class="ruby-keyword kw">in</span> <span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">data_record</span>.<span class="ruby-identifier">length</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>
                                        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">data_record</span>[<span class="ruby-identifier">single_field_value</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword kw">nil</span>
                                                <span class="ruby-identifier">data_record</span>[<span class="ruby-identifier">single_field_value</span>] = <span class="ruby-value str">' '</span>
                                        <span class="ruby-keyword kw">end</span>        
                                        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">single_field_value</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">data_record</span>.<span class="ruby-identifier">length</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>
                                                <span class="ruby-identifier">file_handler</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">data_record</span>[<span class="ruby-identifier">single_field_value</span>]<span class="ruby-operator">+</span><span class="ruby-value str">&quot;,&quot;</span>)
                                        <span class="ruby-keyword kw">else</span>       
                                                <span class="ruby-identifier">file_handler</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">data_record</span>[<span class="ruby-identifier">single_field_value</span>])
                                        <span class="ruby-keyword kw">end</span>
                                <span class="ruby-keyword kw">end</span>
                                <span class="ruby-identifier">file_handler</span>.<span class="ruby-identifier">write</span>(<span class="ruby-value str">&quot;\n&quot;</span>)
                        <span class="ruby-keyword kw">end</span>
                <span class="ruby-ivar">@end_time</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
                <span class="ruby-identifier">generate_extraction_info_file_table_level</span>(<span class="ruby-identifier">new_connection</span>,<span class="ruby-node">&quot;#{table_name}&quot;</span>, <span class="ruby-ivar">@start_time</span>,<span class="ruby-ivar">@end_time</span>)
                <span class="ruby-identifier">table_counter</span> = <span class="ruby-identifier">table_counter</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
                <span class="ruby-identifier">file_handler</span>.<span class="ruby-identifier">close</span>
        <span class="ruby-keyword kw">end</span>
        <span class="ruby-ivar">@end_time_database</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">new</span>
        <span class="ruby-identifier">generate_extraction_info_file_database_level</span>(<span class="ruby-identifier">connection_options</span>[<span class="ruby-value">1</span>],<span class="ruby-identifier">table_counter</span>,(<span class="ruby-ivar">@end_time_database</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@start_time_database</span>))
 <span class="ruby-keyword kw">end</span></pre>
</body>
</html>